package cn.com.blueInfo.bpm.number.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.number.entity.NumberLog;
import cn.com.blueInfo.bpm.number.mapper.NumberLogMapper;
import cn.com.blueInfo.bpm.number.repository.NumberLogRepository;
import cn.com.blueInfo.bpm.number.request.NumberLogDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 编号规则日志表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:26:29
 */
@Log4j2
@Service
public class NumberLogRepositoryImpl
        extends ServiceImpl<NumberLogMapper, NumberLog>
        implements NumberLogRepository, BaseRepository<NumberLog, NumberLogDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增编号规则日志
     * @param numberLog 编号规则日志参数
     */
    @Override
    public Integer createNumberLog(NumberLog numberLog) {
        return sqlExecuteResult(save(numberLog));
    }

    /**
     * 批量新增编号规则日志
     * @param numberLogList 编号规则日志参数列表
     */
    @Override
    public Integer batchCreateNumberLog(List<NumberLog> numberLogList) {
        return sqlExecuteResult(saveBatch(numberLogList), numberLogList.size());
    }

    /**
     * 删除编号规则日志
     * @param numberLogId 编号规则日志ID
     */
    @Override
    public Integer deleteNumberLog(Long numberLogId) {
        NumberLog numberLog = new NumberLog();
        numberLog.setId(numberLogId);
        numberLog.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(numberLog));
    }

    /**
     * 批量删除编号规则日志
     * @param numberLogIdList 编号规则日志ID列表
     */
    @Override
    public Integer batchDeleteNumberLog(List<Long> numberLogIdList) {
        List<NumberLog> numberLogList = new ArrayList<>();
        for (Long numberLogId : numberLogIdList) {
            NumberLog numberLog = new NumberLog();
            numberLog.setId(numberLogId);
            numberLog.setDeleted(BaseDeleted.DELETED);
            numberLogList.add(numberLog);
        }
        return sqlExecuteResult(removeBatchByIds(numberLogList), numberLogList.size());
    }

    /**
     * 更新部分编号规则日志
     * @param numberLog 编号规则日志参数
     */
    @Override
    public Integer updateNumberLogPartial(NumberLog numberLog) {
        validateUpdateId(numberLog.getId());
        LambdaUpdateWrapper<NumberLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NumberLog::getId, numberLog.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部编号规则日志
     * @param numberLog 编号规则日志参数
     */
    @Override
    public Integer updateNumberLog(NumberLog numberLog) {
        validateUpdateId(numberLog.getId());
        return sqlExecuteResult(updateById(numberLog));
    }

    /**
     * 查询一条编号规则日志
     * @param numberLogId 编号规则日志ID
     */
    @Override
    public NumberLog getNumberLogById(Long numberLogId) {
        LambdaQueryWrapper<NumberLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NumberLog::getId, numberLogId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询编号规则日志
     * @param numberLogQueryDTO 编号规则日志参数
     */
    @Override
    public Page<NumberLog> getNumberLogPage(NumberLogDTO.QueryDTO numberLogQueryDTO) {
        Page<NumberLog> numberLogPage = new Page<>(
                numberLogQueryDTO.getPage(), numberLogQueryDTO.getSize());
        LambdaQueryWrapper<NumberLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberLogQueryDTO);

        BaseDeleted deleted = numberLogQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(numberLogPage, queryWrapper);
        } else {
            return page(numberLogPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询编号规则日志数量
     * @param numberLogQueryDTO 编号规则日志参数
     */
    @Override
    public Long getNumberLogCount(NumberLogDTO.QueryDTO numberLogQueryDTO) {
        LambdaQueryWrapper<NumberLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberLogQueryDTO);

        BaseDeleted deleted = numberLogQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询编号规则日志
     * @param numberLogQueryDTO 编号规则日志参数
     */
    @Override
    public List<NumberLog> getNumberLogList(NumberLogDTO.QueryDTO numberLogQueryDTO) {
        LambdaQueryWrapper<NumberLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberLogQueryDTO);

        BaseDeleted deleted = numberLogQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<NumberLog> queryWrapper, NumberLogDTO.QueryDTO numberLogQueryDTO) {

        Long ruleId = numberLogQueryDTO.getRuleId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(ruleId), NumberLog::getRuleId, ruleId);

        String generatedNum = numberLogQueryDTO.getGeneratedNum();
        queryWrapper.eq(StringUtils.isNotBlank(generatedNum), NumberLog::getGeneratedNum, generatedNum);

        String bizType = numberLogQueryDTO.getBizType();
        queryWrapper.eq(StringUtils.isNotBlank(bizType), NumberLog::getBizId, bizType);

        Long bizId = numberLogQueryDTO.getBizId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(bizId), NumberLog::getBizId, bizId);

        String bizTableName = numberLogQueryDTO.getBizTableName();
        queryWrapper.eq(StringUtils.isNotBlank(bizTableName), NumberLog::getBizTableName, bizTableName);

        LocalDateTime generateTime = numberLogQueryDTO.getGenerateTime();
        queryWrapper.eq(ObjectUtils.isNotEmpty(generateTime), NumberLog::getGenerateTime, generateTime);

        Long operatorId = numberLogQueryDTO.getOperatorId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(operatorId), NumberLog::getOperatorId, operatorId);

        addQueryWrapper(queryWrapper, numberLogQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<NumberLog, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                NumberLog::getOrderNumber,
                NumberLog::getTenantId,
                NumberLog::getApplicationId,
                NumberLog::getStatus,
                NumberLog::getDeleted,
                NumberLog::getDeleteUserId,
                NumberLog::getDeleteTime,
                NumberLog::getRemark,
                NumberLog::getCreateUserId,
                NumberLog::getCreateTime,
                NumberLog::getUpdateUserId,
                NumberLog::getUpdateTime,
                NumberLog::getExtend1,
                NumberLog::getExtend2,
                NumberLog::getExtend3);
    }

}
